package com.yuandian.ops.workbench.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.yuandian.api.business_monitor.feign.RemoteAdvancedAlarmService;
import com.yuandian.api.business_monitor.po.AlarmParams;
import com.yuandian.api.business_monitor.vo.BusiAdvancedAlarmVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.poi.ExcelTitleUtils;
import com.yuandian.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 高级告警表 前端控制器
 * </p>
 *
 * @author fucx
 * @since 2020-11-10
 */
@Slf4j
@RestController
@RequestMapping("/alarm/advanced")
@Api(tags = "自定义告警接口")
public class BusiAdvancedAlarmController {

	@Autowired
	private RemoteAdvancedAlarmService busiAdvancedAlarmService;

	/**
	 * 查询自定义告警数据
	 * @param params 参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "自定义告警数据", notes = "自定义告警数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,  dataType = "Integer"),
			@ApiImplicitParam(name = "alarmLevelIds",  		value = "告警等级列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "objectTypeIds", 		value = "告警对象列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", required = true,  dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", required = true,  dataType = "Long"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,  dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,  dataType = "String")
	})
	@PostMapping("/getAdvancedAlarm")
	public R<PageDataInfo> getAdvancedAlarm(@RequestBody AlarmParams params) {
		R<PageDataInfo> result = null;

		// 远程调用接口获取数据
		try {
			long sTime = System.currentTimeMillis();
			result = busiAdvancedAlarmService.getKeyBusinessBySystemId(params);
			log.info("[运维工作台]-[远程调用-BPM服务]-[获取自定义告警数据]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM服务]-[获取自定义告警数据]报错: " + e.getMessage());
		}

		return result;
	}

	/**
	 * 查询自定义告警数据-下载
	 * @param params 参数
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "自定义告警数据-下载", notes = "自定义告警数据-下载")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId",  			value = "系统id",  			paramType = "query", required = true,  dataType = "Integer"),
			@ApiImplicitParam(name = "alarmLevelIds",  		value = "告警等级列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "objectTypeIds", 		value = "告警对象列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "segmentIds", 			value = "网端ID列表",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", required = true,  dataType = "String"),
			@ApiImplicitParam(name = "endTime",   			value = "结束时间", 			paramType = "query", required = true,  dataType = "String")
	})
	@PostMapping("/exportExcel")
	public void exportExcel(@RequestBody AlarmParams params,HttpServletResponse response) {
		//目前高级告警没有按天分表
		String tableName = "ads_busi_custom_alarm_m";
		params.setTableName(tableName);
		PageDataInfo pageDataInfo = null;

		//去掉前后空格
		if(StringUtils.isNotBlank(params.getAlarmContext())) {
			params.setAlarmContext(params.getAlarmContext().trim());
		}

		// 远程调用接口获取数据
		try {
			params.setPageNum(1);
			params.setPageSize(-1);
			long sTime = System.currentTimeMillis();
			pageDataInfo = BeanUtils.cast(busiAdvancedAlarmService.getKeyBusinessBySystemId(params).getData());
			log.info("[运维工作台]-[远程调用-BPM服务]-[自定义告警数据-下载]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-BPM服务]-[自定义告警数据-下载]报错: " + e.getMessage());
		}


		List<BusiAdvancedAlarmVo>  list = new ArrayList<>();
		list = BeanUtil.copyToList(pageDataInfo.getData(), BusiAdvancedAlarmVo.class);

		// 添加序号
		int index = 1;
		for (BusiAdvancedAlarmVo busiAdvancedAlarmVo : list) {
			busiAdvancedAlarmVo.setId(index++);
		}

		//设置大标题
		String title = "自定义告警列表";
		List<String> titleParams = new ArrayList<>();
		titleParams.add(title);
//		titleParams.add(searchOptionService.querySystemName(params.getSystemId()));
		titleParams.add(params.getSystemName()); // TODO 前端再原来基础上新增 'systemName' 请求参数
		String formTitleName = ExcelTitleUtils.formTitleName(titleParams, params.getStartTime(), params.getEndTime());
		//需要展示的字段
		List<String> displayFields = Arrays.asList("序号","告警级别","告警时间","指标","告警对象","业务名称","应用节点","告警信息");
		List<List<String>> headList = new ArrayList<>();
		displayFields.forEach(s -> {
			List<String> childHeadList = new ArrayList<>();
			childHeadList.add(formTitleName);
			childHeadList.add(s);
			headList.add(childHeadList);
		});

		try {
			ExcelUtil.writeExcel(BusiAdvancedAlarmVo.class, "自定义告警表", title, list, headList, response);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("自定义告警列表下载异常!");
		}
	}

}
